package com.zw.controller;

import com.zw.dao.PersistantDao;
import com.zw.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zw.model.GirlPO;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * Created by xlinux on 2017/7/6.
 */
@RestController
@RequestMapping(value = "/crud")
public class CrudController {
    private static final Logger LOG = LogManager.getLogger(CrudController.class);
    @Autowired
    private PersistantDao persistantDao;//项目简单且方便，因此先直接注入
    @Autowired
    private GirlService service;
    /**
     * 连通性测试
     * @param id
     * @return
     */
    @GetMapping(value = "/findGirl/{id}")
    public GirlPO findGirl(@PathVariable("id") Integer id){
        return persistantDao.findOne(id);
    }

    @GetMapping(value = "/login/{name}/{password}")
    public @ResponseBody String login(@PathVariable("name") String name,@PathVariable("password") String password){
        return "username is "+name+" password is"+password;
    }

    @PostMapping(value = "/saveGirl")
    public GirlPO save(@RequestParam("age") int age,@RequestParam("cupSize") String cupSize){
        GirlPO po = new GirlPO();
        po.setAge(age);
        po.setCupSize(cupSize);
        return persistantDao.save(po);
    }

    @PostMapping(value = "/updateGirl")
    public GirlPO updateGirl(@RequestParam("id") int id , @RequestParam("age") int age ,
                           @RequestParam("cupSize") String cupSize){
        GirlPO po = new GirlPO();
        po.setId(id);
        po.setAge(age);
        po.setCupSize(cupSize);
        return persistantDao.save(po);
    }

    @DeleteMapping(value = "deleteGirl")
    public void deleteGirl(@RequestParam("id") int id)
    {
        persistantDao.delete(id);
    }

    /**
     * 事物管理
     */
    @PostMapping(value = "/saveDouble")
    public void saveTwo(){
        service.saveDouble();
    }
}
